<?php

namespace Common\Model;
use Think\Model;

class MovieV3Model extends Model {
    protected $trueTableName = 'tb_movie_v3';

    public function _getWhere($map) {
        $where = array();
        empty($map['name']) ?: $where['name'] = array('like', '%' . $map['name'] . '%');
        empty($map['movie_type']) ?: $where['movie_type'] = $map['movie_type'];
        empty($map['columns']) ?: $where['columns'] = $map['columns'];
        empty($map['country_id']) ?: $where['country_id'] = $map['country_id'];
        empty($map['category_id']) ?: $where['category_id'] = array('like', '%' . $map['category_id'] . '%');
        empty($map['create_time']) ?: $where["FROM_UNIXTIME(create_time,'%Y-%m-%d')"] = $map['create_time'];
        empty($map['st']) ?  : $where["status"] = $map['st'];
        empty($map['keyword']) ?: $where['search_keywords'] = array('like', '%' . $map['keyword'] . '%');

        return $where;

    }

    public function findMovie($map, $page_num = 1, $page_size = 20) {

        $where = $this->_getWhere($map);

        $list = $this
            ->where($where)
            ->order('id desc')
            ->page($page_num . ',' . $page_size)
            ->select();

        return $list;
    }

    public function getMovieNum($map) {
        $where = $this->_getWhere($map);
        $count = M('Movie')->where($where)->count();
        return $count;
    }

    public function getOneMovie($id)
    {
        $oneMovie = $this->where('id =' . $id)->find();
        return $oneMovie;
    }


    //获取所有电影分类
    public function getCategory(){

        $category=$this->getField('category_id',true);

        return $category;
    }


    public function getMovieByCategory($category_id){


        $data=$this
            ->where("category_id=".$category_id." and status=1")
            ->select();

        return $data;
    }


    //利用app端传过来的id获取位置

    public function getPosition($category_id,$id){

        $totalData=$this->where("category_id=".$category_id." and status=1")->getField('id',true);

        if($id==null){

            $position=-1;

        }else{

            $position=array_search($id,$totalData);
        }

        return $position+1;

    }


//2015-12-3 新增白名单功能 $userType  并 修改 where条件
    public function getMovies($category_id,$p,$num,$userType=null){
        $where=array();
        $where['category_id']=$category_id;
        if(!$userType){
            $where['status']=1;
        }

        $data=$this->where($where)->order('sort')->page($p.','.$num)->select();

        return $data;

    }
    
    
    public function getMoviesWithPage($category_id = 1,$userType=null){
        $category_id= $category_id?$category_id:1;
        $where=array();
        if(!$userType){
            $where['tb_movie.status']=1;
            $where['tb_activity.act_status'] ='enabled';
        }
       if($category_id==1){
           $where['is_tv_series'] =0;
           $where['tb_movie.end_time']=array('EGT',time());
           $where['tb_movie.play_time']=array('ELT',time());
       }

        if($category_id ==2){
            $where['tb_movie.is_tv_series'] =0;
            $where['tb_movie.play_time']=array('EGT',time());
        }

        if($category_id ==3){
            $where['tb_movie.is_tv_series'] =1;
            $where['tb_movie.end_time']=array('EGT',time());
        }



    	$data=$this->join('left join tb_activity on tb_movie.id = tb_activity.movie_id')
    	->field('tb_movie.id, tb_movie.name, tb_movie.title, tb_activity.alias, tb_activity.cover')
    	->where($where)->order('tb_movie.sort, tb_activity.order_num')->select();

        //p($this->getLastSql());

    	return $data;
    }

    //获取电影id name
    public function getMoviesIdName()
    {
        $list =$this->where('status =1')->field('id,name')->select();
        return $list;
    }

 public function getIdName()
    {
        $list =$this->where('status =1')->field('id,name')->select();
        return $list;
    }

    public function getAllMovies(){

        $Movies=$this->field('id,name')->select();
        return $Movies;
    }


    //v3 根据栏目获取电影
    public function getMovieV3($columns_type,$userType=null)
    {
        $where=array();
        $where['columns']=$columns_type;
        if(!$userType){
            $where['status']=1;
        }
        $data=$this->where($where)->field('id,name,img_url')->order('sort')->select();
        return $data;
    }

    //v3 根据电影ids 获取电影相关信息
    public function getMovieInIds($ids,$columns_type=1)
    {
        if($ids){
            $ids = trim($ids,',');
            $ids = explode(',',$ids);
            $data=array();
            $movie_type_data =get_system_conf('movie_type');


            foreach($ids as $val){
                $where=array();
                $where['id']=$val;
                $temp_data=array();

                $temp_data = $this->where($where)->field('id,name,img_url,movie_type,columns')->find();

                foreach($movie_type_data as $movie_type_data_key=>$movie_type_data_val){
                    if($movie_type_data_val['country_id'] == $temp_data['movie_type']){
                        $temp_data['name']=$temp_data['name'].'/'.$movie_type_data_val['name'];
                    }
                }

                if($temp_data){
                    //  linux 服务器的物理地址
                    $url = '/var/www'.substr($temp_data['img_url'],stripos($temp_data['img_url'],'51dy.ren')+8);
                    $imageInfo =getimagesize($url);
                    $temp_data['image_width'] = $imageInfo[0];
                    $temp_data['image_height'] = $imageInfo[1];
                    //2013-03-25
                    $temp_data['url']= columnsJumpUrl($temp_data['id']);
                     //var_dump($temp_data);die();
                    unset($temp_data['movie_type']);
                    unset($temp_data['columns']);
                    $data[]=$temp_data;
                }
            }
            if($data){
                return $data;
            }else{
                return null;
            }
        }else{
            return null;
        }
    }

    /**
     * v3 获取一种类电影
     * @param int $columns  电影类型 "1"=>'电影',"2"=>"动漫","3"=>"电视剧","4"=>"综艺","5"=>"脱口秀","6"=>"舞台剧","7"=>"演唱会","9"=>"今日之星"
     * @return mixed
     */
    public function getCartoonIdName($columns=0)
    {
        $where = array();
        $where['status']=1;
        if($columns){
            $where['columns'] = $columns;
        }

        $list =$this->where($where)->field('id,name')->select();

        //var_dump($list);

        return $list;
    }

    /**
     * v3  获取动漫数据
     * @return mixed
     */

    public function getCartoon()
    {
        $where = array();
        $where['columns']=2;
        $where['status']=1;
        $data =$this->where($where)->field(array('id','name','img_url'=>'image'))->select();

        $data_real=array();

        foreach($data as $key=>$val){
            $temp_data=array();
            $temp_data['name']=$val['name'];
            $temp_data['image'] = $val['image'];
            //跳转协议
            $temp_data['url'] = zmbbProtocolCreate(array('name'=>'cartoonNearbyDetail','movie_id'=>$val['id'],'title'=>$val['name']));
            $data_real[]=$temp_data;
        }

       return $data_real;
    }

    public function getTimeList()
    {
        $where=array();
        $where['status']=1;

       $data =  $this->where($where)->field('play_time')->group("FROM_UNIXTIME( play_time, '%Y' )")->select();

        $data_real=array();
        foreach($data as $key =>$val){
            $temp=array();
            $temp['name']= date('Y',$val['play_time']);
            $temp['time']=date('Y',$val['play_time']);
            $data_real[]= $temp;
        }
        return $data_real;
    }



    //v3 根据电影ids 获取电影相关信息
    public function getMovieInIdsForMyFocus($ids)
    {
        if($ids){
            $ids = trim($ids,',');
            $ids = explode(',',$ids);
            $data=array();
            foreach($ids as $val){
                $where=array();
                $where['id']=$val;
                $temp_data=array();

                $temp_data = $this->where($where)->field('id,name,columns,status')->find();


                if($temp_data){

                    if($temp_data['status']==1){
                        $temp_data['open_type']="1"; // 正常
                    }else{
                        $temp_data['open_type']="2";  // 店铺失效
                    }

                    //2013-03-26
                    $temp_data['url']= columnsJumpUrl($temp_data['id']);
                    unset($temp_data['columns']);
                    unset($temp_data['status']);
                    unset($temp_data['id']);
                    $data[]=$temp_data;
                }
            }
            if($data){
                return $data;
            }else{
                return array();
            }
        }else{
            return array();
        }
    }


    //v3 同一个制片方下的更多电影
    public function getMovieByProducer($producer=null){
        if($producer){
            $where = array();
            $where['status']=1;
            $where['producer']=$producer;
            $data = $this->where($where)->select();

            $ids = array_unique(array_column($data,'id'));

            $movie_type_data =get_system_conf('movie_type');

            $data_real=array();

            foreach($ids as $val){
                $where=array();
                $where['id']=$val;
                $temp_data=array();

                $temp_data = $this->where($where)->field('id,name,img_url,movie_type,columns')->find();

                foreach($movie_type_data as $movie_type_data_key=>$movie_type_data_val){
                    if($movie_type_data_val['country_id'] == $temp_data['movie_type']){
                        $temp_data['name']=$temp_data['name'].'/'.$movie_type_data_val['name'];
                    }
                }

                if($temp_data){
                    //  linux 服务器的物理地址
                    $url = '/var/www'.substr($temp_data['img_url'],stripos($temp_data['img_url'],'51dy.ren')+8);
                    $imageInfo =getimagesize($url);
                    $temp_data['image_width'] = $imageInfo[0];
                    $temp_data['image_height'] = $imageInfo[1];
                    //2013-3-25 修改
                    $temp_data['url']= columnsJumpForProduct_v2($temp_data['id'],$temp_data['columns'],$temp_data['name']);
                    //var_dump($temp_data);die();

                    unset($temp_data['movie_type']);
                    unset($temp_data['columns']);
                    $data_real[]=$temp_data;
                }
            }
            if($data_real){
                return $data_real;
            }else{
                return array();
            }
        }else{
            return array();
        }
    }



    //v3 根据电影ids 获取电影相关信息 用于 栏目下面的分类页面
    public function getMovieInIdsForCategory($ids,$columns_type=1)
    {
        if($ids){
            $ids = trim($ids,',');
            $ids = explode(',',$ids);
            $data=array();
            $movie_type_data =get_system_conf('movie_type');

            foreach($ids as $val){
                $where=array();
                $where['id']=$val;
                $temp_data=array();

                $temp_data = $this->where($where)->field('id,name,cover,img_url,movie_type,columns')->find();

                foreach($movie_type_data as $movie_type_data_key=>$movie_type_data_val){
                    if($movie_type_data_val['country_id'] == $temp_data['movie_type']){
                        $temp_data['name']=$temp_data['name'].'/'.$movie_type_data_val['name'];
                    }
                }

                if($temp_data){
                    //  linux 服务器的物理地址

                    if($temp_data['cover']){
                        $url = '/var/www'.substr($temp_data['cover'],stripos($temp_data['cover'],'51dy.ren')+8);
                        $temp_data['img_url']=$temp_data['cover'];
                    }else{
                        $url = '/var/www'.substr($temp_data['img_url'],stripos($temp_data['img_url'],'51dy.ren')+8);

                    }
                    unset($temp_data['cover']);

                    $imageInfo =getimagesize($url);
                    $temp_data['image_width'] = $imageInfo[0];
                    $temp_data['image_height'] = $imageInfo[1];
                    $temp_data['url']= columnsJump($temp_data['id'],$temp_data['columns']);
                    //var_dump($temp_data);die();
                    unset($temp_data['movie_type']);
                    unset($temp_data['columns']);
                    $data[]=$temp_data;
                }
            }
            if($data){
                return $data;
            }else{
                return null;
            }
        }else{
            return null;
        }
    }

    //v3 根据电影ids 获取电影相关信息 用于 栏目下面的分类页面
    //2016-3-25修改
    public function getMovieInIdsForCategory_v2($ids)
    {
        if($ids){
            $ids = trim($ids,',');
            $ids = explode(',',$ids);
            $data=array();
            $movie_type_data =get_system_conf('movie_type');

            foreach($ids as $val){
                $where=array();
                $where['id']=$val;
                $temp_data=array();

                $temp_data = $this->where($where)->field('id,name,cover,img_url,movie_type,columns')->find();

                foreach($movie_type_data as $movie_type_data_key=>$movie_type_data_val){
                    if($movie_type_data_val['country_id'] == $temp_data['movie_type']){
                        $temp_data['name']=$temp_data['name'].'/'.$movie_type_data_val['name'];
                    }
                }

                if($temp_data){
                    //  linux 服务器的物理地址

                    if($temp_data['cover']){
                        $url = '/var/www'.substr($temp_data['cover'],stripos($temp_data['cover'],'51dy.ren')+8);
                        $temp_data['img_url']=$temp_data['cover'];
                    }else{
                        $url = '/var/www'.substr($temp_data['img_url'],stripos($temp_data['img_url'],'51dy.ren')+8);

                    }
                    unset($temp_data['cover']);

                    $imageInfo =getimagesize($url);
                    $temp_data['image_width'] = $imageInfo[0];
                    $temp_data['image_height'] = $imageInfo[1];
                    $temp_data['url']= columnsJumpUrl($temp_data['id']);
                    //var_dump($temp_data);die();
                    unset($temp_data['movie_type']);
                    unset($temp_data['columns']);
                    $data[]=$temp_data;
                }
            }
            if($data){
                return $data;
            }else{
                return null;
            }
        }else{
            return null;
        }
    }


    //2016-4-15
    // 根据电影分类来获取下面的电影
    public function getDataByClassify($classify_id=0)
    {
        $where=array()
        $where['status']=1;
        $where['columns'] =$classify_id;  // 分类
        $data = $this->where($where)->field('id,name,image')->order('name desc')->select();
        if($data){
            return $data;
        }else{
            return array();
        }
    }


}